const app = getApp()

Page({
  data: {
    started: false,
    inputValue: '',
    disable:true,
    debug_data: ''
  }, bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  setDisabled: function (e) {
    this.setData({
      disabled: !this.data.disabled
    })
  },
  start: function () {
    this.setData({
      started: true
    })
    this.selectComponent('#timer').start()
  },
  stop: function () {
    this.setData({
      started: false
    })
    this.selectComponent('#timer').stop()
  },
  printLog: function(res){
    var logs = this.data.debug_data + '\r\n' + res
    this.setData({
      debug_data: logs
    })
  },
  connWebSocket: function (data) {
    wx.showLoading({
      title: 'WebSocket连接中...',
    })
    var that=this
    console.log('testWebSocket')
    wx.connectSocket({
      url: data.detail.value.wssurl,
    })
    wx.onSocketOpen((result) => {
      console.log(JSON.stringify(result))
      wx.hideLoading()
      that.printLog(JSON.stringify(result))
      that.setDisabled()
    })

    wx.onSocketMessage((result) => {
      console.log(JSON.stringify(result))
      that.printLog(JSON.stringify(result))
    })

    wx.onSocketClose((result) => {
      console.log(JSON.stringify(result))
      that.setDisabled()
      that.printLog(JSON.stringify(result))
    })
  },
  sendWebSocket: function () { 
    var nowtime = new Date().getTime()
    wx.sendSocketMessage({
      data: 'I am wechat app....' + nowtime,
    })
  },
  closeWebSocket: function () {
    wx.closeSocket()
  }
})
